<?php
session_start();
require_once 'include/db.php';
require_once 'include/userClass.php';
require_once 'include/postClass.php';
$post = new postClass($user -> getUserID(), $db);
$title = $_GET['usrName']."'s Profil";
include 'include/header.php';

if($user -> isUserAdmin()){			// ONLY Administrator can delete directly from his profile
	if((isset($_POST['cid']) || isset($_POST['postId']) && isset($_POST['delete']))){
		if(isset($_POST['postId'])){ 		// Admin deletes post direct from his profile
			$action = $post -> deletePost($_POST['postId'], $user -> isAdmin());
		}else{						// Admin deletes comment direct from his profile
			$action = $post -> deleteComment($_POST['cid'], $user -> isUserAdmin());
		}
	}
}

if(isset($_POST['usrName'])) {
	if($user->hasReported($_POST['usrName'])) {
		echo "<script type='text/javascript'>
				alert('Du har allerede rapportert denne brukeren');
			</script>";
	}
	else {
		$user->report_user($_POST['usrName'], $_POST['comment']);
	}
}
?>


<div id='content'>
<?php
	if(isset($action)){
		echo "<br /><h4>{$action}</h4><br />";
	}
	if(!$user->isUserAdmin()){							//Display user post, NOT deleted ones
		echo "<h2>".$_GET['usrName']."'s blogg</h2>";
		echo $post -> getPostUsr($_GET['usrName'], $user -> isUserAdmin()); 	
	}else{
		if($_GET['usrName'] != $user -> userName){ 		//Admin displaying userprofile sees all post deleted or not
			echo "<h2>".$_GET['usrName']."'s blogg</h2>";
			echo $post -> getPostUsr($_GET['usrName'], $user -> isUserAdmin());
		}												// Admin displaying his own posts
		else if(isset($_GET['own']) && $_GET['own'] == true) {
			echo "<h2>".$_GET['usrName']."'s blogg</h2>";
			echo $post -> getPostUsr($_GET['usrName'], false);
		}
		else{
			$admin -> getMarkedPosts(); 				//Admin login, get overview over marked posts and comments
			$admin -> getMarkedComments();
		}
	} 
?>
</div>
<div id='side'>
<?php
	if(!$user -> userLoggedOn()){ 					//Lookup user in DB
		$user -> showUserInfo($user -> getUserInfo($_GET['usrName']));
	}
	else if($user -> userName != $_GET['usrName']){
		$user -> showUserInfo($user -> getUserInfo($_GET['usrName']));
		echo <<<HTML
		<a class='button' id='report_button' onclick='document.getElementById("report_form").style.display="block";return false;' href='#' title='Rapporter bruker'><img class='icon' src='include/img/16_Cancel.png' />Rapporter bruker</a><br/>
HTML;
		echo "<div id='report_form' style='display:none'>".
			"<form name='reportform' method='post' action='".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']."'>".
				"<input type='hidden' name='usrName' value='".$_GET['usrName']."' />".
				"<textarea name='comment' cols='20' rows='5' placeholder='Rapportert for...'></textarea>".
				"<input type='submit' value='Rapporter'>".
			"</form></div>";
	}else{											//Get userinfo from user obj
		$user -> showUserInfo($user -> getUserInfo(NULL));
		if($user->isUserAdmin() == true) {
			echo "<a class='button' href='adminStat.php?usrName=".$user->userName."' title='Statistikk'>Brukeroversikt</a><br/>\n
			<a class='button' href='newAdmin.php?usrName=".$user->userName."' title='Ny admin'>Ny administrator</a><br/>
			<a class='button' href='displayUsr.php?usrName=".$user->userName."&own=true' title='Min blogg'>Min blogg</a><br/>";
		}
		echo "<a class='button' href='newPost.php?usrName=".$user->userName."' title='Nytt innlegg'><img class='icon' src='include/img/Edit.png' />Nytt innlegg</a><br/>\n
			<a class='button' href='editUser.php' title='Rediger profil'><img class='icon' src='include/img/Wheel.png' />Rediger profil</a>";
	}
?>

</div>
<?php include 'include/footer.php'; ?>